﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RiskMan.Config;
using XlDde;
using WpfApplication1.Providers;
using RiskMan.Providers;
using RiskMan.Providers.Channels;


namespace TheControllerRisk.Providers
{
    public class DDEProvider
    {
        private XlDdeServer _server;
        public bool isConnected = false;
        private RiskMan.DataClass.Data data;

        public DDEProvider(RiskMan.DataClass.Data data)
        {
            this.data = data;
        }

        public void Connect()
        {
            _server = new XlDdeServer(Cfg.DdeServer);
            
            _server.AddChannel(Cfg.CurrentListChannel, new DDEChannelCurrentList( ) );
            _server.AddChannel(Cfg.DealsListChannel, new DDEChannelDealList());
            _server.AddChannel(Cfg.TableLimiteToEmitent, new DDEChannelTableLimiteByEmitent());
            _server.AddChannel(Cfg.PositionToClientAccountChannel, new DDEChannelPositionToClientAccount());
            _server.AddChannel(Cfg.StopOrderChannel, new DDEChannelStopOrderList() );
            _server.AddChannel(Cfg.LinkedOrderChannEl, new DDEChannelOrderList());
            _server.AddChannel(Cfg.LimiteByAccount, new DDEChannelLimiteByAccount());
            _server.AddChannel("portfel", new DDEChannelPortfel());
            _server.AddChannel("limitebymoneysredstvam", new DDEChannelMoneySredstva());
            _server.AddChannel(Cfg.AllTradesChannel, new DDEChannelAllTrades());

            _server.Register();
            isConnected = true;
        }

        public void Disconnect()
        {
            if (_server != null)
            {
                _server.Unregister();
                _server.Disconnect();
                _server.Dispose();
                _server = null;
                isConnected = false;
            }
        }
    }
}
